<?php

namespace App\Services\Draw\PoolGroupObject;

use App\Exceptions\BasicException;
use App\Jobs\QiPao\Draw\GashaponDrawFullGiftJob;
use App\Repositories\LogRepository;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;

/**
 *
 * @author wrh
 */
class GashaponPublicPoolGroup extends PublicPoolGroup
{

    public function getPoolPrizeList($user)
    {
        return new Collection();
    }

    public function checkDrawRoundPrizeCount(Collection $poolPrizeList, int $drawCount, $user, $poolConfig)
    {
        $templatePrizeCount = $this->poolTemplateModel->sumTemplateCount(['group_id' => $this->group_id]);
        $prizesLength       = $this->redisObject->llen($this->cachePrefix . "prizeList_" . $this->tag);
        //获取模板礼物总数 2023.12.10由 /2 改为/10
        if ($templatePrizeCount / 10 + $drawCount > $prizesLength) {
            return false;
        }
        return true;
    }

    public function createNextRoundPrizes(Collection $templatePrizes, $user, int $drawCount)
    {
        if (!$this->redisObject->set($this->cachePrefix . "fullPollLock_" . $this->tag, 1, 'ex', $this->gameLockTime, 'nx')) {
            return null;
        }
        try {
            $prizesLength = $this->redisObject->llen($this->cachePrefix . "prizeList_" . $this->tag);

            //判断奖池剩余数量 如果这次抽奖数量都不足够则同步补充礼物
            if (false && $drawCount < $prizesLength) {

                $poolGroupInfo = ['group_id' => $this->group_id, 'pool_id' => $this->pool_id, 'tag' => $this->tag];
                dispatch(new GashaponDrawFullGiftJob($templatePrizes, $poolGroupInfo, $this->cachePrefix))->onQueue('isGamePush');
            } else {
                if (!$this->createNextRoundPrizesWeight($templatePrizes)) {
                    return [];
                }
            }

            //补充补池金额
            $this->addPoolRepo->checkPool($this->tag, 2, $this->group_id);
        } catch (\Exception $e) {
            throw $e;
        }
        // 清空本轮礼物总数
//        $this->poolPrizeModel->updateData(['group_id' => $this->group_id], ['template_count' => 0]);
//        $this->poolPrizeModel->appendPrizes($this, $templatePrizes);
    }

    /*public function drawPrizes(Collection $poolPrizeList, $poolConfig, $poolGroup, $user, $drawCount)
    {
        $groupPrizes = $this->redisObject->pipeline(function ($pipe) use ($drawCount) {
            for ($i = 0; $i < $drawCount; $i++) {
                $pipe->lpop($this->cachePrefix . "prizeList_" . $this->tag);
            }
        });

        $groupPrizes = array_filter($groupPrizes);
        $drawPrizes = [];
        app('events')->listen('draw.redisRollback', function () use ($groupPrizes) {
            if (!empty($groupPrizes)) {
                $this->redisObject->lpush($this->cachePrefix . "prizeList_" . $this->tag, array_reverse($groupPrizes));
            }
        });

        if (count($groupPrizes) != $drawCount) {
            if (!empty($groupPrizes)) {
                $this->redisObject->lpush($this->cachePrefix . "prizeList_" . $this->tag, array_reverse($groupPrizes));
            }
            throw new BasicException(-1, '服务繁忙,请稍后再试');
        }
        foreach ($groupPrizes as $prize) {
            $prizeData = explode('_', $prize);
            if (!isset($drawPrizes[$prizeData[0]][$prizeData[1]])) {
                $drawPrizes[$prizeData[0]][$prizeData[1]] = 1;
            } else {
                $drawPrizes[$prizeData[0]][$prizeData[1]]++;
            }
        }
        return $drawPrizes;
    }*/

    public function decPoolPrize(array $drawPrizes)
    {
        $this->poolPrizeModel->batchDecPrizesByGroup($this, $drawPrizes);
        return true;
    }

}
